// TelegramDataController.java
package com.wangfugui.mybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangfugui.mybatisplus.cls.TelegramData;
import com.wangfugui.mybatisplus.cls.TelegramDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/telegram")
public class TelegramDataController {

    @Autowired
    private TelegramDataService telegramDataService;

    /**
     * 获取最近100条TelegramData的title，按序号换行输出
     */
    @GetMapping(value = "/recent-titles", produces = "text/plain;charset=UTF-8")
    public String getRecentTitles() {
        try {
            // 创建查询条件，按id降序排列（最近的数据）
            QueryWrapper<TelegramData> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("title");
            queryWrapper.orderByDesc("publish_time");

            // 分页查询最近100条数据
            Page<TelegramData> page = new Page<>(1, 100);
            Page<TelegramData> resultPage = telegramDataService.page(page, queryWrapper);

            List<TelegramData> telegramList = resultPage.getRecords();

            // 按序号格式化输出
            StringBuilder output = new StringBuilder();
            for (int i = 0; i < telegramList.size(); i++) {
                TelegramData data = telegramList.get(i);
                output.append((i + 1)).append(". ").append(data.getTitle()).append("\n");
            }

            return output.toString();
        } catch (Exception e) {
            log.error("获取最近title列表时出错: ", e);
            return "获取数据失败: " + e.getMessage();
        }
    }
    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal("0.5");
        int x = bigDecimal.compareTo(new BigDecimal("1"));
        System.out.println(x);
    }
}
